package vn.appsmobi.ui;

import vn.appsmobi.R;
import vn.appsmobi.activity.CategoryAppActivity;
import vn.appsmobi.activity.CategoryTabActivity;
import vn.appsmobi.adapter.CategoryAdapter;
import vn.appsmobi.loader.BaseLoader;
import vn.appsmobi.loader.CategoryLoader;
import vn.appsmobi.loader.CategoryLoader.Result;
import vn.appsmobi.model.CategoryInfo;
import vn.appsmobi.ulti.Constant;
import vn.appsmobi.ulti.Log;
import vn.appsmobi.widget.EmptyLoadingView;
import vn.appsmobi.widget.PageScrollListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class CategoryFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<CategoryLoader.Result>
{
    private static final int CATEGORY_LOADER = 0;
    private static final int GRIDVIEW_COLUMN_COUNT = 1;
    private CategoryAdapter mCategoryAdapter;
    private ListView mListView;
    private EmptyLoadingView mLoadingView;
    private String mCategoryId;
    
    private AdapterView.OnItemClickListener mItemClickListner = new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            if (position < CategoryFragment.this.mListView.getChildCount());
            {
                CategoryInfo categoryInfo = (CategoryInfo)CategoryFragment.this.mCategoryAdapter.getItem(position);
                
                Toast.makeText(CategoryFragment.this.getActivity().getBaseContext(),categoryInfo.getName(),Toast.LENGTH_SHORT).show();
                Intent intent;
                if(categoryInfo.getTotalCount()> 0)
                {
                    intent = new Intent(CategoryFragment.this.getActivity(),CategoryTabActivity.class);
                }
                else
                {
                    intent = new Intent(CategoryFragment.this.getActivity(),CategoryAppActivity.class);
                }
                intent.putExtra(Constant.INTENT_EXTRA_CATEGORY_PARENT_ID, categoryInfo.getCategoryId());
                intent.putExtra(Constant.INTENT_EXTRA_CATEGORY_PARENT_NAME, categoryInfo.getName());                
                CategoryFragment.this.getActivity().startActivity(intent);
                CategoryFragment.this.getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.disappear);
               
            }
        }
    };
    
    
    public void setCategory(String catid)
    {
        this.mCategoryId = catid;
    }
    
    
    public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
    {
        
        Log.v("category fragment");
        View localView = paramLayoutInflater.inflate(R.layout.category_fragment, paramViewGroup, false);
        this.mListView = ((ListView)localView.findViewById(R.id.list_view));
        this.mCategoryAdapter = new CategoryAdapter(getActivity(), GRIDVIEW_COLUMN_COUNT);
        this.mListView.setAdapter(this.mCategoryAdapter);
        this.mListView.setOnItemClickListener(this.mItemClickListner);
        this.mListView.setOnScrollListener(new PageScrollListener(null));        
        this.mLoadingView = ((EmptyLoadingView)localView.findViewById(R.id.loading));
        return localView;
    }
    
    
    
    
    /**Phong
	 * 
	 */
	public CategoryFragment() {
		super();
		// TODO Auto-generated constructor stub
	}


	public void onActivityCreated(Bundle paramBundle)
    {
        super.onActivityCreated(paramBundle);
        getLoaderManager().initLoader(CATEGORY_LOADER, null, this);
        // initLoader(CATEGORY_LOADER,null,this,getLoaderManager());
    }
    @Override
    public void onStart(){
        super.onStart();
        
    }
    
    @Override
    public Loader onCreateLoader(int id, Bundle bundle) {
        Log.v("onCreateLoader called");
        if (id == CATEGORY_LOADER)
        {
            this.mLoader = new CategoryLoader(getActivity());
            ((CategoryLoader)this.mLoader).setCategoryID(this.mCategoryId);
            this.mLoader.setProgressNotifiable(this.mLoadingView);
        }
        BaseLoader baseLoader = this.mLoader;
        return baseLoader;
    }
    
    
    
    @Override
    public void onLoaderReset(Loader<Result> arg0) {
        // TODO Auto-generated method stub
        
    }
    
    
    @Override
    public void onLoadFinished(Loader<Result> loader, Result data) {
        // TODO Auto-generated method stub
        Log.v("onLoadFinished");
        this.mCategoryAdapter.updateData(data.mCategoryInfos);
    }
    
    
}